<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/lib/layui/css/layui.css"  media="all">
</head>
<body>


<table class="layui-hide" id="test" lay-filter="test"></table>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">购买</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/lib/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var $ = layui.jquery;
        var table = layui.table;
        var cartTable = table.render({
            elem: '#test'
            ,url:'/cart/findAll.do'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left',hide:true}
                ,{field:'goodsId', title:'商品编号',  edit: 'text'}
                ,{field:'goodsName', title:'商品名字',  edit: 'text'}
                ,{field:'goodsPrice', title:'商品价格',  sort: true, }
                ,{field:'goodsNum', title:'商品数量',  edit: 'text', sort: true}
                ,{field:'cartId', title:'购物车ID',  sort: true,hide:true}
                ,{field:'flag', title:'库存', templet: function(res){
                        return  res.flag===1?"有货":"无货"
                    }}
                ,{field:'createBy', title:'创建人'}
                ,{field:'createTime', title:'创建时间',templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                ,{field:'lastUpdateTime', title:'修改时间',hide:true,templet : "<div>{{layui.util.toDateString(d.lastUpdateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                ,{field:'money', title:'总价'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true
            ,done:function (rec) {
                if (rec.code === 2) {
                    layer.msg("无权访问购物车", {icon: 5, time: 1500},function () {
                        window.location.href = "/html/welcome.html?t=1";
                    });
                }
            }
        });


        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/cart/delCart.do",
                        dataType:"json",
                        type:"post",
                        data:{
                            id:data.id,
                        },
                        success:function (r) {
                            if(r.code==0){
                                alert("删除成功")
                                cartTable.reload()
                            }else {
                                alert("删除失败")
                            }
                        }
                    })

                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                $.ajax({
                    url:"/orders/addOrders.do",
                    dataType:"json",
                    type:"post",
                    data:{
                        goodsId:data.goodsId,
                        goodsName:data.goodsName,
                        goodsPrice:data.goodsPrice,
                        goodsNum:data.goodsNum,
                        createBy:data.createBy,
                        money:data.money,
                        cid:data.id,
                    },
                    success:function (r) {
                        if(r.code==0){
                            // cartTable.reload()
                            location.href="/html/shopping/settlement.html?goodsId="+data.goodsId+"&goodsName="+data.goodsName+"&createBy="+data.createBy+"&money="+data.money
                        }
                    }
                })

            }
        });


    });
</script>
</body>
</html>